<!DOCTYPE html>
<!--
  ~ JBoss, Home of Professional Open Source.
  ~ Copyright 2013, Red Hat, Inc., and individual contributors
  ~ as indicated by the @author tags. See the copyright.txt file in the
  ~ distribution for a full listing of individual contributors.
  ~
  ~ This is free software; you can redistribute it and/or modify it
  ~ under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ This software is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with this software; if not, write to the Free
  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  -->

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:t="http://myfaces.apache.org/tomahawk">

<h:head>
    <title>Narayana Transaction Analyser #{switchLogBean.currentLogFile}</title>
    <h:outputStylesheet library="css" name="bootstrap.min.css"/>
    <style>
        body {
            padding-top: 10px;
        }
        body .modal {
            /* new custom width */
            width: 800px;
            /* must be half of the width, minus scrollbar on the left (30px) */
            margin-left: -400px;
        }
    </style>
</h:head>

<h:body>
    <div class="container-fluid">
        <div class="navbar">
            <div class="navbar-inner">
                <h:link value="Narayana Transaction Analyser" class="brand" outcome="txlist"/>
                <h:form>
                    <h:commandButton id="trace_logging" value="#{traceLoggingBean.status}" class="btn pull-right"
                                     action="#{traceLoggingBean.setTraceLoggingEnable(traceLoggingBean.traceLoggingEnable)}">
                        <f:ajax render="@this"/>
                    </h:commandButton>
                    <a id="btn-switchlog" href="#myModal" class="btn pull-right" role="button" data-toggle="modal">Switch log</a>
                </h:form>
            </div>
        </div>
        <div class="row-fluid" id="content">
            <div class="span8">
                <h:form class="form-inline">
                    <div>
                        <h4>Filter</h4>
                        <h:outputLabel for="filterByStatus" value="By Status: "/>
                        <h:selectOneMenu id="filterByStatus" value="#{transactionListBean.filterByStatus}">
                            <f:selectItem itemLabel="ALL" itemValue=""/>
                            <f:selectItems value="#{transactionListBean.statuses}"/>
                            <f:ajax render="top_prevPage top_page_links top_nextPage txlist bottom_prevPage bottom_page_links bottom_nextPage page" event="change" execute="filterByStatus"/>
                        </h:selectOneMenu>
                    </div>
                    <div id="top_paging">
                        <h:commandButton id="top_prevPage" value="&lt; Newer" action="#{transactionListBean.prevPage}"
                                         class="btn" disabled="#{transactionListBean.currentPage == 0}"
                                         rendered="#{transactionListBean.totalPage > 1}">
                            <f:ajax render="top_prevPage top_page_links top_nextPage txlist bottom_prevPage bottom_page_links bottom_nextPage page"/>
                        </h:commandButton>
                        <div class="pagination" style="margin: 10px; display: inline-table;">
                            <t:dataList value="#{transactionListBean.pages}" id="top_page_links" var="page" layout="unorderedList"
                                        rowCountVar="rcnt" rowIndexVar="rindex"
                                        rendered="#{transactionListBean.totalPage > 1}">
                                <h:commandLink value="..." rendered="#{rindex == 1 and page != 2}" disabled="true"/>
                                <h:commandLink value="#{page}" actionListener="#{transactionListBean.selectPage}"
                                               disabled="#{page == transactionListBean.currentPage + 1}"/>
                                <h:commandLink value="..." rendered="#{rindex == rcnt - 2 and page != transactionListBean.totalPage - 1}" disabled="true"/>
                            </t:dataList>
                        </div>
                        <h:commandButton id="top_nextPage" value="Older &gt;" action="#{transactionListBean.nextPage}"
                                         class="btn" disabled="#{transactionListBean.currentPage == transactionListBean.totalPage - 1}"
                                         rendered="#{transactionListBean.totalPage > 1}">
                            <f:ajax render="top_prevPage top_page_links top_nextPage txlist bottom_prevPage bottom_page_links bottom_nextPage page"/>
                        </h:commandButton>
                    </div>
                    <div id="transactions">
                        <h:dataTable value="#{transactionListBean.transactionsList}" id="txlist" var="transaction"
                                     rendered="#{not empty transactionListBean.transactionsList}"
                                     styleClass="table table-striped">
                            <h:column>
                                <f:facet name="header">Transaction ID</f:facet>
                                <h:link value="#{transaction.txuid}" outcome="txinfo">
                                    <f:param name="includeViewParams" value="true"/>
                                    <f:param name="txid" value="#{transaction.id}"/>
                                </h:link>
                            </h:column>
                            <h:column>
                                <f:facet name="header">Node</f:facet>
                                #{transaction.nodeid}
                            </h:column>
                            <h:column>
                                <f:facet name="header">Distributed</f:facet>
                                #{transaction.distributed}
                            </h:column>
                            <h:column>
                                <f:facet name="header">Status</f:facet>
                                #{transaction.status}
                            </h:column>
                            <h:column>
                                <f:facet name="header">Duration</f:facet>
                                #{transaction.duration}ms
                            </h:column>
                        </h:dataTable>
                    </div>
                    <div id="bottom_paging">
                        <h:commandButton id="bottom_prevPage" value="&lt; Newer" action="#{transactionListBean.prevPage}"
                                         class="btn" disabled="#{transactionListBean.currentPage == 0}"
                                         rendered="#{transactionListBean.totalPage > 1}">
                            <f:ajax render="top_prevPage top_page_links top_nextPage txlist bottom_prevPage bottom_page_links bottom_nextPage page"/>
                        </h:commandButton>
                        <div class="pagination" style="margin: 10px; display: inline-table;">
                            <t:dataList value="#{transactionListBean.pages}" id="bottom_page_links" var="page" layout="unorderedList"
                                        rowCountVar="rcnt" rowIndexVar="rindex"
                                        rendered="#{transactionListBean.totalPage > 1}">
                                <h:commandLink value="..." rendered="#{rindex == 1 and page != 2}" disabled="true"/>
                                <h:commandLink value="#{page}" actionListener="#{transactionListBean.selectPage}"
                                               disabled="#{page == transactionListBean.currentPage + 1}"/>
                                <h:commandLink value="..." rendered="#{rindex == rcnt - 2 and page != transactionListBean.totalPage - 1}" disabled="true"/>
                            </t:dataList>
                        </div>
                        <h:commandButton id="bottom_nextPage" value="Older &gt;" action="#{transactionListBean.nextPage}"
                                         class="btn" disabled="#{transactionListBean.currentPage == transactionListBean.totalPage - 1}"
                                         rendered="#{transactionListBean.totalPage > 1}">
                            <f:ajax render="top_prevPage top_page_links top_nextPage txlist bottom_prevPage bottom_page_links bottom_nextPage page"/>
                        </h:commandButton>
                    </div>
                    <h:outputText id="page" value="Page #{transactionListBean.currentPage + 1} / #{transactionListBean.totalPage}"
                                  rendered="#{transactionListBean.totalPage > 1}"/>
                </h:form>
            </div>
            <div class="span4" id="issues">
                <ui:repeat value="#{issueParserBean.issues}" var="issue">
                    <div class="alert alert-info">
                        <button type="button" class="close" data-dismiss="alert">&#215;</button>
                        <p><strong>
                            <h:outputText value="#{issue.title}" escape="false"/>
                        </strong></p>
                        <p>
                            <h:outputText value="#{issue.body}" escape="false"/>
                        </p>
                    </div>
                </ui:repeat>
            </div>
        </div>
    </div>

    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            Switch Log
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&#215;</button>
        </div>
        <div class="modal-body" id="change-log">
            <h:form enctype="multipart/form-data">
                <h:selectOneRadio value="#{switchLogBean.currentLogFile}" layout="pageDirection">
                    <f:selectItems value="#{switchLogBean.logFiles}" />
                    <f:selectItem itemValue="upload" itemLabel="upload" />
                </h:selectOneRadio>
                <t:inputFileUpload value="#{switchLogBean.uploadedFile}" />
                <div class="alert alert-info">
					<p>Only files less than 20M in size can be uploaded.<br/>For larger files, copy them to the $JBOSS_HOME/standalone/data/nta/log/ directory. <br/>Then restart the server and when you revisit this page, the new log file(s) should appear in this list for selection.</p>
                </div>
                <h:commandButton value="Change" action="#{switchLogBean.changeLog}" />
                <h:messages />
            </h:form>
        </div>
    </div>

    <h:outputScript library="js" name="jquery-1.9.1.min.js"/>
    <h:outputScript library="js" name="bootstrap.min.js"/>

    <script>
        var POLL_INTERVAL = 5000;
        $(function () {
            setInterval(
                    function () {
                        $('#content').load(document.URL + ' #content');
                        console.log(document.URL);
                    },
                    POLL_INTERVAL
            )
        })
    </script>
</h:body>
</html>
